package com.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.mapper.SysMenusMapper;
import com.lin.pojo.PageVo;
import com.lin.pojo.SysMenus;
import com.lin.service.SysMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SysMenusServiceImpl extends ServiceImpl<SysMenusMapper, SysMenus>implements SysMenusService {
    @Autowired
    SysMenusMapper sysMenusMapper;

    @Override
    public List<SysMenus> querysysmenusparentid() {
        List<SysMenus> sysMenus=sysMenusMapper.querysysmenusparentid();
        for(SysMenus sysMenus1: sysMenus){

            sysMenus1.setSysMenus(sysMenusMapper.querylistsysmenusbyid(sysMenus1.getId()));
            for (SysMenus list1: sysMenus){
                System.out.println(list1);
            }
        }
        return sysMenus;
    }

    @Override
    public Map addSysMenus(SysMenus sysMenus) {
        sysMenus.setStatus("0");
        boolean num = this.save(sysMenus);
        SysMenus sysMenus2=new SysMenus();
        sysMenus2.setSeq(sysMenus.getId());
        sysMenus2.setId(sysMenus.getId());
        this.updateById(sysMenus2);
        Map map =new HashMap();
        map.put("code","0");
        map.put("msg","添加失败");
        if(num){
            map.put("code","1");
            map.put("msg","添加成功");
        }
        return map;
    }

        @Override
        public Map delSysMenus(int id) {
            SysMenus sysMenus=new SysMenus();
            sysMenus.setId(id);
            sysMenus.setStatus("1");
            boolean num = this.updateById(sysMenus);
            Map map = new HashMap();
            map.put("code", "0");
            map.put("msg", "删除失败");
            if (num) {
                map.put("code", "1");
                map.put("msg", "删除成功");
            }
            return map;
        }

        @Override
        public Map updateSysMenus(SysMenus sysMenus) {
            boolean num = this.updateById(sysMenus);
            Map map = new HashMap();
            map.put("code", "0");
            map.put("msg", "修改失败");
            if (num) {
                map.put("code", "1");
                map.put("msg", "修改成功");
            }
            return map;
        }

    @Override
    public List<SysMenus> queryMenuByfid(int pid) {
        return sysMenusMapper.queryMenuByfid(pid);
    }


    @Override
        public PageVo<SysMenus> querySysMenusbycond(SysMenus sysMenus, int pageno, int pagesize) {
            Page<SysMenus> page = new Page<SysMenus>(pageno, pagesize);
            //条件查询
            QueryWrapper<SysMenus> queryWrapper = new QueryWrapper<SysMenus>();
            queryWrapper.eq("status",0);
            if (StringUtils.isNotBlank(sysMenus.getName())) {
                queryWrapper.like("name", sysMenus.getName());
            }

            Page<SysMenus> page1 = this.page(page, queryWrapper);
            PageVo<SysMenus> pageVo = new PageVo<SysMenus>();
            pageVo.setTotal(page1.getTotal());
            pageVo.setRows(page1.getRecords());
            return pageVo;
        }
    }
